<?php
/**
 *  模块管理操作类
 */
class osgh_admin_index_module_cls 
{
	private static $osgh_admin_group_tbl_this        = NULL;
	private static $osgh_admin_index_tbl_this        = NULL;
	private static $osgh_admin_module_tbl_this       = NULL;
	private static $osgh_admin_id_module_tbl_this    = NULL;
	private static $osgh_admin_module_group_tbl_this = NULL;
	/**
	 * 
	 * 构造函数
	 */
	function __construct()
	{
		self::$osgh_admin_group_tbl_this  == NULL && self::$osgh_admin_group_tbl_this = new osgh_admin_group_tbl_cls();
		self::$osgh_admin_index_tbl_this == NULL && self::$osgh_admin_index_tbl_this = new osgh_admin_index_tbl_cls();
		self::$osgh_admin_module_tbl_this == NULL && self::$osgh_admin_module_tbl_this = new osgh_admin_module_tbl_cls();
		self::$osgh_admin_id_module_tbl_this == NULL && self::$osgh_admin_id_module_tbl_this = new osgh_admin_id_module_tbl_cls();
		self::$osgh_admin_module_group_tbl_this == NULL && self::$osgh_admin_module_group_tbl_this = new osgh_admin_module_group_tbl_cls();
	}
	
	/**
	 * 获取用户模块列表
	 * @param int $user_id
	 * @return array
	 */
	public function getAdminIndexModuel($user_id, $menu_level = 0, $pid = 0)
	{
		$user_id    = intval($user_id);
		$menu_level = intval($menu_level);
		$pid        = intval($pid);
		
		if ($user_id < 1 ) return array();
		
		$admin_index_info  = self::$osgh_admin_index_tbl_this->getAdminIndexInfo($user_id);
		if( empty($admin_index_info) || $admin_index_info['is_available'] != 1 ) return array();
		
		$module_ids = array();	
		
		
		$admin_id_module_list = self::$osgh_admin_id_module_tbl_this->getAdminIdModuleList(false, "admin_id={$user_id}");
		foreach ($admin_id_module_list as $admin_id_module)
		{
		    $module_ids[] = $admin_id_module['module_id'];
		}
		
		//取得该组的所以权限
		$admin_module_group_obj =  new osgh_admin_module_group_tbl_cls();
		$admin_id_module_list_list = $admin_module_group_obj->getAdminModuleGroupList(false, "group_id={$admin_index_info['group_id']}");
		foreach ($admin_id_module_list_list as $admin_id_module)
		{
		    $module_ids[] = $admin_id_module['module_id'];
		}
		
		if( empty($module_ids) ) return array();
		
		array_unique($module_ids);
		
		$where_return = implode(',', $module_ids);
		
		$where = strlen($where_return) > 0 ? " module_id in ({$where_return}) " : '';
		
		if ($menu_level > 0)
		{
		    if( strlen($where) > 0 ) $where .=  " AND ";
		    $where .= "menu_level = ".$menu_level;
		}
		
		if ($pid > 0)
		{
		    if( strlen($where) > 0 ) $where .=  " AND ";
		    $where .= "parent_id = " . $pid ;
		}

		return self::$osgh_admin_module_tbl_this->getAdminModuleList(false, $where,  'rank asc', '');		
	}	
        
         /**
         * 判断这个操作是否有权限
         * 
         * return bool
         */        
        public function is_have_warrant($admin_id,$model)
        { 
                if( !empty($admin_id) && ($model =='index' || $model =='index_menu' || $model =='index_menu_load')) return true;          
               
                $admin_id    = intval($admin_id);				
		if ($admin_id < 1 ) return array();
		
		$admin_index_info  = self::$osgh_admin_index_tbl_this->getAdminIndexInfo($admin_id);
		if( empty($admin_index_info) || $admin_index_info['is_available'] != 1 ) return array();
                $module_ids = array();
                //取得个人特别的权限
		$admin_id_module_list = self::$osgh_admin_id_module_tbl_this->getAdminIdModuleList(false, "admin_id={$admin_id}");
		foreach ($admin_id_module_list as $admin_id_module)
		{
		    $module_ids[] = $admin_id_module['module_id'];
		}		
		//取得该组的所以权限
		$admin_module_group_obj =  new osgh_admin_module_group_tbl_cls();
		$admin_id_module_list_list = $admin_module_group_obj->getAdminModuleGroupList(false, "group_id={$admin_index_info['group_id']}");
		foreach ($admin_id_module_list_list as $admin_id_module)
		{
		    $module_ids[] = $admin_id_module['module_id'];
		}
                
                $module_obj = new osgh_admin_module_tbl_cls(); 
                foreach($module_ids as $module_id )
                {
                       $module_info = $module_obj->getAdminModuleInfo($module_id);
                       if($module_info['module_code'] == $model ) return true;
                }
                return false;
        } 
        	
	/**
	 * 
	 * 获取人员管理列表
	 * @param int $group_id
	 */
	public function getGroupIndexList($group_id)
	{
		if (intval($group_id) > 0)
		{
			$admin_list = self::$osgh_admin_index_tbl_this->getAdminIndexList(false, "group_id={$group_id}");
			foreach ($admin_list as $admin_key => $admin)
			{
				$admin['is_available']? $admin_list[$admin_key]['status'] = TRUE : $admin_list[$admin_key]['status'] = false;
			}
			return $admin_list;
		}		
		return array();
	}
		
	
	/**
	 * 
	 * 获取用户组模块列表
	 * @param int $group_id
	 */
	public function getAdminGroupModuleList($group_id)
	{
		$group_id = intval($group_id);
		if($group_id > 0)
		{
			//获取模块列表
			$module_list = self::$osgh_admin_module_tbl_this->getAdminModuleList();
			
			//获取所在院线的模块列表
			$admin_module_array = array();
			$group_module_list = self::$osgh_admin_module_group_tbl_this->getAdminModuleGroupList(false, "group_id={$group_id}");
			foreach ($group_module_list as $group_module)
			{
				in_array($group_module['module_id'], $admin_module_array) || $admin_module_array[] = $group_module['module_id'];
			}			
			$group_module_array = array();
			foreach ($module_list as $row)
			{
				if ( in_array($row['module_id'], $admin_module_array) )
				{
					$row['status'] = "have";
					$row['special'] = "0";
				}
				else
				{
					$row['status']  = "no";
					$row['special'] = "0";
				}
				$group_module_array[] = $row;
			}
			
			return $group_module_array;
		}
		return array();
	} 
	
	/**
	 * 
	 * 更新用户组模块
	 * @param int $group_id
	 * @param array $add_arry
	 * @param array $del_arry
	 */
	public function upAdminGroupModule($group_id, $add_arry = array(), $del_arry = array())
	{
		$return = FALSE;
		if (intval($group_id) > 0)
		{
			if (!empty($add_arry))
			{
				foreach ($add_arry as $add_module)
				{
					$add_data = array('group_id' => $group_id, 'module_id' => $add_module);
					self::$osgh_admin_module_group_tbl_this->addAdminModuleGroup($add_data);
					$return = true;
				}
			}
			
			if (!empty($del_arry))
			{
				foreach ($del_arry as $del_module)
				{
					$return = self::$osgh_admin_module_group_tbl_this->delAdminModuleGroup($group_id, $del_module);
				}
			}
		}
		return  $return;
	}
	
	/**
	 * 
	 * 更新用户模块
	 * @param int $admin_id
	 * @param array $add_arry
	 * @param array $del_arry
	 */
	public function upAdminIdModule($admin_id, $add_arry = array(), $del_arry = array())
	{
		$return = FALSE;
		if (intval($admin_id) > 0)
		{
			if (!empty($add_arry))
			{
				foreach ($add_arry as $add_module)
				{
					$add_data = array('admin_id' => $admin_id, 'module_id' => $add_module);
					self::$osgh_admin_id_module_tbl_this->addAdminModule($add_data);
					$return = true;
				}
			}
			
			if (!empty($del_arry))
			{
				foreach ($del_arry as $del_module)
				{
					$return = self::$osgh_admin_id_module_tbl_this->delAdminModule($del_module, $admin_id);
				}
			}
		}
		return  $return;
	}
	
}